package com.sugon.controller.module;

import com.sugon.annotation.AutoLog;
import com.sugon.entity.ModuleRecordEntity;
import com.sugon.service.ModuleRecordService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import com.sugon.utils.RRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 改良改善台账Controller
 *
 * @author sugon
 * @date 2019-08-08 08:37:29
 */
@Controller
@RequestMapping("modulerecord")
public class ModuleRecordController {
    @Autowired
    private ModuleRecordService moduleRecordService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("modulerecord:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ModuleRecordEntity> moduleRecordList = moduleRecordService.queryList(query);
        int total = moduleRecordService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(moduleRecordList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("modulerecord:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ModuleRecordEntity moduleRecord = moduleRecordService.queryObject(id);

        return R.ok().put("moduleRecord", moduleRecord);
    }

    /**
     * 保存
     */
    @AutoLog("改良改善台账--保存")
    @RequestMapping("/save")
    @RequiresPermissions("modulerecord:save")
    @ResponseBody
    public R save(@RequestBody ModuleRecordEntity moduleRecord) {
        moduleRecordService.save(moduleRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @AutoLog("改良改善台账--修改")
    @RequestMapping("/update")
    @RequiresPermissions("modulerecord:update")
    @ResponseBody
    public R update(@RequestBody ModuleRecordEntity moduleRecord) {
        moduleRecordService.update(moduleRecord);

        return R.ok();
    }

    /**
     * 审核
     */
    @AutoLog("改良改善台账--修改")
    @RequestMapping("/check")
    @RequiresPermissions("modulerecord:check")
    @ResponseBody
    public R check(@RequestBody ModuleRecordEntity moduleRecord) {
        moduleRecordService.check(moduleRecord);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ModuleRecordEntity> list = moduleRecordService.queryList(params);

        return R.ok().put("list", list);
    }


    /**
     * 删除
     */
    @AutoLog("改良改善台账--删除")
    @RequestMapping("/delete")
    @RequiresPermissions("modulerecord:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        moduleRecordService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * excel导入改良改善
     */
    @RequestMapping("/importGLGS")
    @RequiresPermissions("modulerecord:import")
    @ResponseBody
    public R importGLGS(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }

        int count = moduleRecordService.importE(file, 0);

        return R.ok("成功导入" + count + "条数据");
    }

    /**
     * 导入OA系统合理化建议板块导出的模具改良改善数据
     */
    @RequestMapping("/importMoldGLGS")
    @RequiresPermissions("devfile:imports")
    @ResponseBody
    public R importMoldGLGS(@RequestParam("file") MultipartFile file) throws Exception {
        int num = moduleRecordService.importMoldGLGS(file);
        return R.ok("成功导入" + num + "记录");
    }

    /**
     * excel导入自主维修
     */
    @RequestMapping("/importZZWX")
    @RequiresPermissions("modulerecord:import")
    @ResponseBody
    public R importZZWX(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }

        int count = moduleRecordService.importE(file, 1);

        return R.ok("成功导入" + count + "条数据");
    }
}
